package com.apiclient.pojo;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.apiclient.annotion.ApiClient;
import com.apiclient.annotion.MethodClient;
import com.apiclient.until.SecretUntil;
import com.google.gson.Gson;
//import com.lmx.project.until.SecretUntil;

import java.util.HashMap;
import java.util.Map;


@ApiClient(interfanceHost = "http://127.0.0.1:8123")
public class LmApiClient {
    private  String accessKey;
    private  String secretKey;
    //   所有请求通用的请求头
//    private

    private LmApiClient() {

    }


    public LmApiClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;

    }

    /**
     * 以get方式调用
     */
    @MethodClient(api = "/name",method = "GET")
    public String getNameByGet(String name) {
        String url = "http://localhost:8090/name";
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", name);
//        String result = HttpUtil.get(url, hashMap);
        HttpResponse execute = HttpRequest.get(url)
                .form(hashMap)
                .addHeaders(getHeaderMap(name))
                .execute();
        System.out.println(execute.getStatus());
        String result = execute.body();
        System.out.println(result);
        return result;
    }

    /**
     * 以post方式调用
     */
    @MethodClient(api = "/name",method = "POST")
    public String getNameByPost(String name) {
//        String url = "http://localhost:8123/name";
        String url = "http://localhost:8090/name";
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", name);
        HttpResponse execute = HttpRequest.post(url)
                .addHeaders(getHeaderMap(name))
                .form(hashMap)
                .execute();

//        String result = HttpUtil.post(url, hashMap);
        System.out.println(execute.getStatus());
        String result = execute.body();
        System.out.println(result);

        return result;
    }

    /**
     * 以 post requestbody的形式调用
     */
    @MethodClient(api = "/name/namejson",method = "POST")
    public String getNameTojSonBypost(User user) {
        HashMap<String, String> headerMap = getHeaderMap(user);

        String json = JSONUtil.toJsonStr(user);
//        String url = "http://localhost:8123/name/namejson";
        String url = "http://127.0.0.1:8090/name/namejson";

        HttpResponse execute = HttpRequest.post(url)
                .addHeaders(headerMap)
                .body(json)
                .execute();

        int status = execute.getStatus();
        System.out.println("请求状态:" + status);
        String body = execute.body();
        System.out.println(body);
        return body;

    }

    private HashMap<String, String> getHeaderMap(Object body) {
        HashMap<String, String> headerMap = new HashMap<>();
        headerMap.put("accessKey", this.accessKey);
        // 生成四位的随机字符串
        headerMap.put("noce", RandomUtil.randomNumbers(4));
        Gson gson = new Gson();
        String bodystr=body.toString();
//        if (RegxUntil.isChinese(body.toString())){
             bodystr=RegxUntil.toPinyin(body.toString());
//        }
        System.out.println(bodystr);
        headerMap.put("body", bodystr);
//     当前的时间戳
        headerMap.put("timestamp", "" + (System.currentTimeMillis() / 100000));
        LmApiClient lmApiClient = new LmApiClient();
        ApiClient client = lmApiClient.getClass().getAnnotation(ApiClient.class);
        headerMap.put("interfancehost",client.interfanceHost());
//        根据秘钥生成sign ,使用当前参数的json字符串与secretKey加密
        headerMap.put("sign", SecretUntil.getSign(bodystr, this.secretKey));
        return headerMap;
    }

    public static void main(String[] args) {
        LmApiClient lmApiClient = new LmApiClient("lmx", "abcdefg");
        User user = new User();

        user.setName("李满祥");

        System.out.println(lmApiClient.getNameByGet("lmx"));
    }
}
